<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DbHandle
 *
 * @author HOANGPHUC
 */
class MySQLProvider {

    var $servername = "localhost";
    var $username = "root";
    var $password = "";
    var $databasename = "DatTranh";
    var $connect;

    function getServerName() {
        return $this->servername;
    }

    function setServerName($serverName) {
        $this->servername = $serverName;
    }

    function getUsername() {
        return $this->username;
    }

    function setUsername($username) {
        $this->username = $username;
    }

    function getPassword() {
        return $this->password;
    }

    function setPassword($password) {
        $this->password = $password;
    }

    function getDatabaseName() {
        return $this->databasename;
    }

    function setDatabaseName($databaseName) {
        $this->databasename = $databaseName;
    }

    function GetConnection() {
        $this->connect = mysql_connect($this->servername, $this->username, $this->password);
        if (!$this->connect) {
            die('Could not connect MySQL: ' . mysql_error());
        }
        return $this->connect;
    }

    function CloseConnection() {
        mysql_close($this->connect);
    }

    function GetResultSet($select) {
        $this->connect = $this->GetConnection();
        mysql_select_db($this->databasename, $this->connect);
        mysql_query("SET NAMES 'utf8' ");
        $result = mysql_query($select, $this->connect);
        return $result;
    }
    function test()
    {
         $this->connect = $this->GetConnection();
        mysql_select_db($this->databasename, $this->connect);
        mysql_query("SET NAMES 'utf8' ");
        $result = mysql_query("select * from theloai", $this->connect);
        while($row = mysql_fetch_array($result))
        {
            echo $row['ID'];
        }
    }
    function Insert($table, $column, $value) {

        $this->connect = $this->GetConnection();
        mysql_select_db($this->databasename, $this->connect);
        mysql_query("SET NAMES 'utf8'");
        $insert = "insert into " . $table . "(";
        for ($i = 0; $i < count($column); $i++) {
            $insert .= $column[$i] . ",";
        }

        $insert = substr($insert, 0, strlen($insert) - 1);
        $insert .= ")values('";
       
        for ($i = 0; $i < count($value); $i++) {
            $insert .= $value[$i] . "','";
        }
        
        $insert = substr($insert, 0, strlen($insert) - 2);
        $insert .= ")";
        echo $insert;
        $result = mysql_query($insert, $this->connect);
        return $result;
    }

    function Update($table, $column, $value, $whereColumn, $whereValue) {
        $this->connect = $this->GetConnection();
        mysql_select_db($this->databasename, $this->connect);
        mysql_query("SET NAMES 'utf8'");
        $update = "update " . $table . " set ";
        for ($i = 0; $i < count($column); $i++) {
            $update .= $column[$i] . "='" . $value[$i] . "',";
        }
        $update = substr($update, 0, strlen($update) - 1);
        $update .= "where " . $whereColumn . "='";
        $update .= $whereValue . "'";
        $result = mysql_query($update, $this->connect);
        echo $update;
        return $result;
    }

    function Delete($table, $whereColumn, $whereValue) {
        $this->connect = $this->GetConnection();
        mysql_select_db($this->databasename, $this->connect);
        mysql_query(" SET NAMES 'utf8'");
        $delete = "delete from " . $table;
        $delete .= " where " . $whereColumn . "='";
        $delete .= $whereValue . "'";
        $result = mysql_query($delete, $this->connect);
        echo $delete;
        return $result;
    }

}

?>
